package rocks.georgik.sdlapp;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

import net.alhazmy13.mediapicker.Video.VideoPicker;

import java.util.List;

public class MainActivity extends Activity {
    SurfaceHolder surfaceHolder;
    SurfaceView surfaceView;
    private VideoView videoView;
    private Button button;
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        surfaceView = (SurfaceView) findViewById(R.id.videoView);
        surfaceHolder = surfaceView.getHolder();

        videoView = (VideoView) surfaceView;

        int permission = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
        if (permission != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 2);
        }
    }

    public void startSDLActivity(View view) {
        Intent intent = new Intent(this, SDLActivity.class);
        startActivity(intent);
    }

    public void startVideo(View view) {
        pickVideo();
    }

    private void pickVideo() {
        new VideoPicker.Builder(MainActivity.this)
                .mode(VideoPicker.Mode.GALLERY)
                .directory(VideoPicker.Directory.DEFAULT)
                .enableDebuggingMode(true)
                .build();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 2) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                startActivity(new Intent(this, SDLActivity.class));
            } else {
                finish();
            }
        }
    }

    //
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d(TAG, "onActivityResult() called with: requestCode = [" + requestCode + "], resultCode = [" + resultCode + "], data = [" + data + "]");
        if (requestCode == VideoPicker.VIDEO_PICKER_REQUEST_CODE && resultCode == RESULT_OK) {
            List<String> path = data.getStringArrayListExtra(VideoPicker.EXTRA_VIDEO_PATH);
            loadVideo(path);
        }
    }

    private void loadVideo(List<String> path) {
        if (path != null && path.size() > 0) {
            String videoPath = path.get(0);
            Log.d(TAG, "path = " + path + ",videoPath = " + videoPath);
            Ffmpeg.playVideo(surfaceHolder.getSurface(), videoPath);
        }
    }

}
